﻿@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_LayoutChild.cshtml";
}
@model Agro.Models.T_Message
@section head
{
    <script type="text/javascript"
     src="@Url.Content("~/Scripts/jquery.uploadify-3.1.js")"  ></script>
    <link href="@Url.Content("~/Content/uploadify/uploadify.css")"     rel="stylesheet" type="text/css" />
    <style type="text/css">
        .uploadify-button
        {
            background-color: transparent;
            border: none;
            padding: 0;
        }
        .uploadify:hover .uploadify-button
        {
            background-color: transparent;
        }
        .uploadbtn
        {
            background: url(/content/upload.gif);
            width: 90px;
            height: 30px;
            background-color: transparent;
            border: none;
            padding: 0;
        }
        .uploadify-queue
        {
            position: absolute;
            left: 10px;
            top: 5px;
            width: 500px;
        }
        .b-col
        {
            width: 500px;
        }
        .col-input
        {
            width: 300px;
        }
        #File_Title
        {
            width: 300px;
        }
    </style>
    <script language="javascript" type="text/javascript">
        var type = '@ViewBag.MessageType';
        var mapData = '';
        var formid='';
        $(document).ready(function () {
            if (type == "add")//如果是新发消息，显示附件上传按钮，否则显示附件下载链接
            {
                initUpload();
                $("#downloadfile").css('display', 'none');
                $("#btnImport").css('margin-top', '10px');
            }
            else {
                $("#btnImport").css('display', 'none');
                $("#downloadfile").attr("href", "javascript:download('" + $("#Message_ID").val()+"')");
                $("#downloadfile").html($("#Message_AttachmentName").val() + " " + $("#Message_AttachmentSize").val() + "");
            }
        });

        function download(id) {
            $("#frmDownFile").attr("src", '/SystemManage/Message/DownLoadFile?id=' + id);
            //            window.location.href = '/FileManage/File/DownLoadFile?id=' + id;

        }

        function Save(t) {
            formid = t;
            var receive = $("#Message_ReceiveOrgid").val();
            if (receive == null || receive == "" || receive=="0") {
                parent.asyncbox.tips('接收单位不能为空!', 'error');
                return;
            }
            var title = $("#Message_Title").val();
            if (title == "" || title == null) {
                parent.asyncbox.tips('消息标题不能为空!', 'error');
                return;
            }
            var title = $("#Message_Content").val();
            if (title == "" || title == null) {
                parent.asyncbox.tips('消息内容不能为空!', 'error');
                return;
            }
            //开始保存
            parent.$.cover(true);
            showparentloading();
            

            //此处判断有无需要上传的文件
            var fileNum = $("#btnImport-queue .uploadify-queue-item").length;
            //如果有需要上传的文件，调用上传的同时，保存页面
            if (fileNum > 0) {
                $('#btnImport').uploadify('upload');
            }
            else {
             //如果没有上传的文件，直接保存页面
                $.funSaveForm("/SystemManage/Message/MessageSave", "formDetail", function (d) {
                    hideparentloading();
                    parent.$.cover(false);
                    if (d == undefined || d == "") {
                        parent.asyncbox.tips('发送失败!', 'error');
                        return;
                    }
                    else {
                        parent.asyncbox.tips('发送成功!', 'success');
                        MainFrame().$(MainFrame().gridId).trigger("reloadGrid");
                        parent.$.close(t.id);
                        return;
                    }
                });
            }
           
            
        }

        function setEmpty() {
            $("#name").val("");
            $("#Message_ReceiveOrgid").val(""); //接收单位
        }

        function selectOragize() {
            var u = "/SystemManage/Organization/SelectOrganize";
            parent.asyncbox.open({
                url: u,
                data: '',
                title: "选择单位",
                width: 300,
                height: 400,
                scroll: 'no',
                modal: true,
                btnsbar: parent.$.btn.OKCANCEL, //按钮栏配置请参考 “辅助函数” 中的 $.btn。
                callback: function (action, opener) {
                    if (action == 'ok') {
                        var nodes = opener._nodes;
                        var OrgIds = "";
                        var OrgNames = "";
                        for (var i = 0; i < nodes.length; i++) {
                            OrgIds += nodes[i].ID + ",";
                            OrgNames += nodes[i].NAME + ",";
                        }
                        if (OrgIds.length > 0) {
                            OrgIds = OrgIds.substring(0, OrgIds.length - 1);
                            OrgNames = OrgNames.substring(0, OrgNames.length - 1);
                        }
                        $("#name").val(OrgNames);
                        $("#Message_ReceiveOrgid").val(OrgIds); //接收单位
                        //
                    }
                    if (action == 'cancel') {
                        return;
                    }
                }
            });

        }
        function initUpload() {
            $("#btnImport").uploadify({
                'formData': { 'ASPSESSID': '@(Session.SessionID)', 'AUTHID': '@ViewBag.auth', GUID: '@ViewBag.GUID' },
                'multi': false,
                'auto': false, //设为true自动上传，false需要触发事件上传
                'swf': '/Content/uploadify/uploadify.swf',
                'uploader': '/SystemManage/Message/MessageSave',
                'width': 90,
                'fileTypeExts': '*.*',
                'height': 22,
                'fileSizeLimit': '10240KB', //设置上传文件的大小10M
                'queueSizeLimit': 1,
                'buttonImage': '/Content/import.gif',
                'onUploadError': function (file, errorCode, errorMsg, errorString) {
                    parent.asyncbox.alert('文件上传失败:' + errorString, '提示');
                    parent.$.cover(false);
                    hideparentloading();
                },
                'onUploadStart': function (file) {//开始上传事件
                    showparentloading();
                    parent.$.cover(true);
                    //将form的值加入到formdata中， 这样在controller中，只需要一个方法，即可处理文件上传和消息内容保存
                    mapData = $("#formDetail").serializeJson();
                    $('#btnImport').uploadify('settings', 'formData', mapData);
                },

                'onUploadSuccess': function (file, data, response) {
                    parent.$.cover(false);
                    hideparentloading();
                    var d = $.parseJSON(data);
                    if (d.Data == undefined || d.Data == "") {
                        parent.asyncbox.alert('发送失败!', '提示');

                    }
                    else {
                        parent.asyncbox.tips('发送成功!', 'success');
                        MainFrame().$(MainFrame().gridId).trigger("reloadGrid");
                        $('#btnImport').uploadify('destroy');
                        //
                    }
                },
                'onDestroy': function () {
                //关闭当前对话框
                    parent.$.close(formid.id);
                }

            });
        }
       
    </script>
}
<!--主窗体，页面都需要的div-->
<div class="ui-layout-center" style="overflow: hidden;">
    @using (Html.BeginForm("", "", FormMethod.Post, new Dictionary<string, object> { { "id", "formDetail" } }))
    {
        @Html.HiddenFor(m => m.Message_ID)
        @Html.HiddenFor(m => m.Message_ReceiveOrgid)
        @Html.HiddenFor(m => m.Message_AttachmentName)
        @Html.HiddenFor(m => m.Message_AttachmentSize)
       <!-- <input id="ASPSESSID" name="ASPSESSID" type="hidden" value="@(Session.SessionID)" /> 
        <input id="AUTHID" name="AUTHID" type="hidden" value="@ViewBag.auth" /> 
        <input id="GUID" name="GUID" type="hidden" value="@ViewBag.GUID" /> 
        -->
        <table class="ui-widget-content" id="t1" style="width: 100%; height: 380px;">
            <tr>
                <td valign="top">
                    <table>
                        <tr>
                            <td align="right" style="width: 100px;">
                                <font color="red">*</font>接收单位：
                            </td>
                            <td>
                                <input type="text" name="name" style="width: 150px;" disabled="disabled" id="name" value='@ViewBag.ReceiveOrgName'/>
                                <input type="button" value="选择.." onclick="selectOragize();" />
                                <input type="button" value="置空" onclick="setEmpty();" />
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <font color="red">*</font>消息标题：
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.Message_Title, new { style = "width:300px;" })
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <font color="red">*</font>消息内容：
                            </td>
                            <td>
                                @Html.TextAreaFor(m => m.Message_Content, new { style = "width:300px;height: 150px;" })
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                附件：
                            </td>
                            <td style="position: relative; display: block;">
                                <input type="file" name="btnImport" id="btnImport" />
                                <a id="downloadfile"></a>
                            </td>
                        </tr>

                        <tr>
                            <td align="right">
                                发送单位：
                            </td>
                            <td>
                        @Html.TextBox("SendOrgName", (string)ViewBag.SendOrgName, new { @disabled="disabled",style = "width:300px;" })
                            </td>
                        </tr>

                        <tr>
                            <td align="right">
                                发送人：
                            </td>
                            <td>
                        @Html.TextBox("CreateUserName", (string)ViewBag.CreateUserName, new { @disabled = "disabled", style = "width:300px;" })
                            </td>
                        </tr>

                    </table>
                </td>
            </tr>
        </table>
    }
</div>
<iframe id='frmDownFile' frameborder='0' width='0' height='0' marginheight='0' marginwidth='0'
    scrolling="auto" style="margin: 0px; margin-bottom: -2px;"></iframe>